//
//  ProdCateViewCell.m
//  JiaoX
//
//  Created by 尹桥印 on 14/11/18.
//  Copyright (c) 2014年 北京娇羞科技有限公司. All rights reserved.
//

#import "ProdCateHViewCell.h"
#import "UIImageView+WebCache.h"
#import "ProdPromotion.h"
#import "Present.h"

#define PROD_PRMT_FONT_SIZE [DimensUtils getScaleX:12]



@interface ProdCateHViewCell()
{
    UIImageView *_prodImage;
    
    UILabel *_prodName;
    UILabel *_nowPrice;
    UILabel *_buyCount;
    UIButton *_giftBtn;
    
    UIView *_prodPrmtView;//限时抢购的view
    UILabel *_prodPrmtLabel;//限时抢购的label
    UILabel *_prodPrmtPriceLabel;//限时抢购的价格的label
}


@end

@implementation ProdCateHViewCell
+ (instancetype)cellWithTableView:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath{
    ProdCateHViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PROD_IDENTIFIER forIndexPath:indexPath];
    return cell;
}

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if(self){
        //添加一个imageView
        UIImageView *prodImage = [[UIImageView alloc]init];
        CGFloat width = (JXScreenW-3*JXPublicInset)*0.5;
        prodImage.frame = CGRectMake(0, 0, width, width);
        _prodImage = prodImage;
        [self addSubview:prodImage];
        
        _prodPrmtLabel = [[UILabel alloc]init];
        [_prodPrmtLabel setFont:[UIFont systemFontOfSize:PROD_PRMT_FONT_SIZE]];
        [_prodPrmtLabel setText:@"限时抢购"];
        [_prodPrmtLabel setTextColor:[UIColor whiteColor]];
        CGSize prmtSize = [_prodPrmtLabel.text sizeWithFont:_prodPrmtLabel.font];
        [_prodPrmtLabel setFrame:CGRectMake(5, 5, prmtSize.width, prmtSize.height)];
        
        _prodPrmtPriceLabel = [[UILabel alloc]init];
        [_prodPrmtPriceLabel setFont:[UIFont systemFontOfSize:PROD_PRMT_FONT_SIZE]];
        [_prodPrmtPriceLabel setTextColor:[UIColor whiteColor]];
        [_prodPrmtPriceLabel setHeight:_prodPrmtPriceLabel.font.lineHeight];
        [_prodPrmtPriceLabel setY:5];
        
        _prodPrmtView = [[UIView alloc]init];
        [_prodPrmtView setSize:CGSizeMake(width, _prodPrmtPriceLabel.font.lineHeight+JXPublicInset)];
        [_prodPrmtView setY:CGRectGetMaxY(prodImage.frame)-_prodPrmtView.height];
        [_prodPrmtView setHidden:YES];
        
        [_prodPrmtView setBackgroundColor:JXColorAlpha(248, 53, 142, 150)];
        
        [_prodPrmtView addSubview:_prodPrmtLabel];
        [_prodPrmtView addSubview:_prodPrmtPriceLabel];
        
        [self addSubview:_prodPrmtView];
        
        
        //添加商品名字
        UILabel *prodName = [[UILabel alloc]init];
        prodName.font = ProdNameFont;
        [prodName setTextColor:JXColor(51, 51, 51)];
        prodName.numberOfLines  =1;
        prodName.frame = CGRectMake(5, width+JXPublicInset, width-5, [prodName.font lineHeight]);
        _prodName = prodName;
        [self addSubview:prodName];
        
        
        //添加商品现价
        UILabel *nowPrice = [[UILabel alloc]init];
        nowPrice.font = ProdNewPriceFont;
        nowPrice.textColor = JXThemeColor;
        _nowPrice = nowPrice;
        [self addSubview:nowPrice];
        
        //添加讨论人数
        UILabel *buyCount = [[UILabel alloc]init];
        buyCount.font = ProdBuyCountFont;
        buyCount.textColor = JXColor(102, 102, 102);
        _buyCount = buyCount;
        [self addSubview:buyCount];
        
        //添加赠
        UIButton *giftBtn = [[UIButton alloc]init];
        giftBtn.titleLabel.font = ProdGiftFont;
        giftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        giftBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        [giftBtn setTitleColor:JXColor(102, 102, 102) forState:UIControlStateNormal];
        [giftBtn setImage:[UIImage imageNamed:@"product_gift"] forState:UIControlStateNormal];
        giftBtn.hidden = true;
        
        _giftBtn = giftBtn;
        
        [self addSubview:giftBtn];

        
        self.backgroundColor = [UIColor whiteColor];
        
        //设置layer
        CALayer *layer=[self layer];
        //设置边框线的宽
        [layer setBorderWidth:1];
        //设置边框线的颜色
        [layer setBorderColor:[JXColor(242, 242, 242) CGColor]];
        
    }
    return self;
}
/**
 *  设置商品列表数据
 *
 *  @param prod  商品列表数据
 *  @param isAdd 是否以最高高度显示，用于赠品显示
 */
-(void)setProdData:(ProductItemInfo *)prod isAdd:(BOOL)isAdd{
    //设置商品图片
    [_prodImage sd_setImageWithURL:[NSURL URLWithString:prod.prodImage]];
    //设置商品名称
    [_prodName setText:prod.prodWholeName];
    //设置商品价格
    [_nowPrice setText:prod.prodNewPrice];
    CGSize nowPriceSize = [prod.prodNewPrice sizeWithFont:ProdNewPriceFont];
    
    _nowPrice.frame =CGRectMake(5, CGRectGetMaxY(_prodName.frame)+JXPublicInset-3, nowPriceSize.width, _nowPrice.font.lineHeight);
    
    _giftBtn.frame = CGRectMake(5, CGRectGetMaxY(_nowPrice.frame)+JXPublicInset/2, self.width-JXPublicInset, 17);
    
    if(isAdd){
        self.bounds = CGRectMake(0, 0, self.width, PRODCATEHVIEW_LARGE_HEIGHT);
    }
    if(prod.prodPromotion!=nil){
        //判断是否有价格促销信息
        if (![Utils isBlankString:prod.prodPromotion.price]) {
            //显示抢购价格
            [_prodPrmtPriceLabel setText:[NSString stringWithFormat:@"￥%@",prod.prodPromotion.price]];
            CGSize prmtPriceSize = [_prodPrmtPriceLabel.text sizeWithFont:_prodPrmtPriceLabel.font];
            [_prodPrmtPriceLabel setX:_prodImage.width-prmtPriceSize.width-5];
            [_prodPrmtPriceLabel setWidth:prmtPriceSize.width];
            [_prodPrmtView setHidden:NO];
        }else{
            //隐藏
            [_prodPrmtView setHidden:YES];
        }
        if(prod.prodPromotion.presentList.count){
                _giftBtn.hidden = NO;
                Present *present = prod.prodPromotion.presentList.firstObject;
                [_giftBtn setTitle:present.name forState:UIControlStateNormal];
                self.bounds = CGRectMake(0, 0, self.width, PRODCATEHVIEW_LARGE_HEIGHT);
        }else{
                _giftBtn.hidden = YES;
                self.bounds = CGRectMake(0, 0, self.width, PRODCATEHVIEW_SMALL_HEIGHT);
        }
    }else{
        _giftBtn.hidden = YES;
        [_prodPrmtView setHidden:YES];
    }
    
    
    //设置商品购买人数
    [_buyCount setText:prod.prodBuyCount];
    CGSize buyCountSize =[prod.prodBuyCount sizeWithFont:ProdBuyCountFont];
    CGFloat comtCountX = self.width-buyCountSize.width-JXPublicInset/2;
    CGFloat comtCountY = CGRectGetMaxY(_nowPrice.frame)-buyCountSize.height-2;
    _buyCount.frame = CGRectMake(comtCountX, comtCountY, self.width-JXPublicInset/2-comtCountX, buyCountSize.height);
}


@end
